<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录界面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        .login-container h2 {
            margin-bottom: 20px;
            color: #333;
        }

        .login-container input[type="text"],
        .login-container input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .login-container button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .login-container button:hover {
            background-color: #45a049;
        }

        .error-message {
            color: red;
            margin-top: 10px;
            display: none;
        }

        .inline-radio {
            display: inline-block;
            margin-right: 10px; /* 可选：增加RadioButton之间的间距 */
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>登录</h2>
    <input type="text" id="username" placeholder="用户名">
    <input type="password" id="password" placeholder="密码">
    <label class="inline-radio">
        <input type="radio" name="option" value="0" checked="checked"> 管理员
    </label>
    <label class="inline-radio">
        <input type="radio" name="option" value="1"> 学生
    </label><br><br>
    <button onclick="login()">登录</button>
    <div class="error-message" id="error-message">登录失败，请重试。</div>
</div>

<script>
    function login() {
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        //获取登录的角色，默认为管理员登录
        const radios = document.getElementsByName('option');
        let loginRole = 0;
        for (let radio of radios) {
            if (radio.checked) {
                loginRole = Number(radio.value);
                break;
            }
        }
        console.log("login Role: " + loginRole);

        const errorMessage = document.getElementById('error-message');

        if (!username || !password) {
            errorMessage.style.display = 'block';
            return;
        }
        let url = 'http://localhost:8090/user/login?u=' + username + '&p=' + password;
        let roleName = "管理员";
        if (loginRole === 1) {
            //学生登录
            url += "&role=1";
            roleName = "学生";
        }

        const xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                // 假设服务器返回JSON格式的响应
                console.log(xhr.responseText);
                //const response = JSON.parse(xhr.responseText);

                if (xhr.responseText === username) {
                    alert(roleName + '[' + username + ']登录成功！');
                    // 这里可以添加登录成功后的逻辑，比如跳转到另一个页面
                    if(loginRole === 0){
                        //跳转到管理员界面
                        window.location.href = "main.html";
                    }else{
                        //跳转到学生界面
                        window.location.href = "student.html?no="+username;
                    }

                } else {
                    errorMessage.style.display = 'block';
                    errorMessage.innerText = xhr.responseText;
                }
            } else if (xhr.readyState === 4) {
                // 处理请求失败的情况
                errorMessage.style.display = 'block';
            }
        };
        const data = JSON.stringify({username: username, password: password});
        xhr.send(data);
    }
</script>
</body>
</html>